package com.itheima.shiro;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * 自定义Realm
 * Created by brss on 2018/6/25.
 */
public class UserRealm extends AuthorizingRealm{
    /**
     * 执行授权逻辑
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行授权逻辑");
        return null;
    }

    /**
     * 执行认证逻辑
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行认证逻辑");
        //假设数据库的用户名：
        String name = "root";
        String password = "root";
        //编写shiro判断逻辑，判断用户名和密码
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        // 1. 判断用户名
        if ( !token.getUsername().equals(name) ){
            //用户名不存在
            return null;// shiro底层会抛出UnKnowAccountException
        }
        // 2.判断密码
        // 第一个参数：返回给页面的数据，第二个参数：数据库密码，第三个参数：realm的名字
        return new SimpleAuthenticationInfo("",password,"");
    }
}
